﻿using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

//using ISP.Xrm;
//using Microsoft.Xrm.Client;
//using System.Linq;

namespace em_crm
{
	[Activity(Label = "@string/ApplicationName", Icon = "@drawable/icon")]
	public class Main : Activity,SlidingDrawer.IOnDrawerCloseListener, SlidingDrawer.IOnDrawerOpenListener 
	{
		ListView drawerList;
		private Button sliderButton;
		private SlidingDrawer drawer;
		private Context context; 

		protected override void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);
			try
			{
			// Activate the action bar and display it in navigation mode.
			RequestWindowFeature(WindowFeatures.ActionBar);

			// Set our view from the "main" layout resource
			//SetContentView(Resource.Layout.activity_main);
			SetContentView(Resource.Layout.OrderFragment);
			// Initialize the action bar
			InitializeActionBar();

			drawer = FindViewById<SlidingDrawer>(Resource.Id.left_drawer);
			drawerList = FindViewById<ListView>(Resource.Id.left_List);
			//sliderButton = FindViewById<Button>(Resource.Id.slideButton);
			//Button btn1 = FindViewById<Button>(Resource.Id.Button01);
			//Button btn2 = FindViewById<Button>(Resource.Id.Button02);
			//Button btn3 = FindViewById<Button>(Resource.Id.Button03);
			//context = btn1.Context;

			//btn1.Click += BtnPressed;
			//btn2.Click += BtnPressed;
			//btn3.Click += BtnPressed;
			
				drawer.SetOnDrawerOpenListener(this);
				drawer.SetOnDrawerCloseListener(this);

				CrmConnection con = new CrmConnection(@"Authentication Type=AD; Server=http://10.203.107.126/ISP; User ID=EG01\crmadmin; Password=Etisalat4444");
				ISPDataContext db = new ISPDataContext(con);

				isp_orders order = (from o in db.isp_orderses
				                    where o.isp_name == "1340-3"
				                    select o).FirstOrDefault();


				TextView tv = FindViewById<EditText>(Resource.Id.OrderNumber);
				tv.Text = order.isp_name;					   
				
			}
			catch (Exception ex)
			{
 				
			}
		}

		public override bool OnCreateOptionsMenu(IMenu menu)
		{
			MenuInflater.Inflate(Resource.Menu.main, menu);
			return true;
		}

		public override bool OnOptionsItemSelected(IMenuItem item)
		{
			//if (mDrawerToggle.onOptionsItemSelected(item)) {
			//return true;
		//}
		//final Activity act = this;
			switch (item.ItemId)
			{
				case global::Android.Resource.Id.Home:

					if (drawer.IsOpened)
						drawer.Close();
					else
						drawer.Open();
					return true;
			}
			//TODO: Handle the selection event here.
			return false;
		}

		private void InitializeActionBar()
		{
			/*
			// Set the action bar to Tab mode
			ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

			// Create a new tab (CreateTabSpec on the TabControl)
			var homeTab = ActionBar.NewTab();

			homeTab.SetTabListener(
				new TabListener<CrumbsOverviewFragment>());

			homeTab.SetText("Crumbs");

			// Add the new tab to the action bar
			ActionBar.AddTab(homeTab);
		*/
			  }
		public void OnDrawerOpened()
		{
			drawer.AnimateOpen();
			sliderButton.Text = "V";
		}

		public void OnDrawerClosed()
		{
			drawer.AnimateClose();
			sliderButton.Text = "^";
		}


		private void BtnPressed(object s, EventArgs e)
		{
			Button t = (Button)s;
			Toast.MakeText(context, t.Text + " was clicked",
	 ToastLength.Short).Show();
		} 
	}
}

